#include "../mqserver/message.hpp"
#include <gtest/gtest.h>

MessageManager::ptr mmp;

class MessageTest : public testing::Environment {
public:
    virtual void SetUp() override {
        mmp = std::make_shared<MessageManager>("./data/message");
        mmp->initQueueMessage("queue1");
    }
    virtual void TearDown() override {
        //mmp->clear();
    }
};


/*
//3.恢复历史数据测试
TEST(MessageTest, recovery_test){
    ASSERT_EQ(mmp->getable_count("queue1"), 4);
    MessagePtr msg1 = mmp->front("queue1");
    ASSERT_NE(msg1.get(), nullptr);
    ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));
    ASSERT_EQ(mmp->getable_count("queue1"), 3);
    ASSERT_EQ(mmp->waitack_count("queue1"),1);

    MessagePtr msg2 = mmp->front("queue1");
    ASSERT_NE(msg2.get(), nullptr);
    ASSERT_EQ(msg2->payload().body(), std::string("Hello World-2"));
    ASSERT_EQ(mmp->getable_count("queue1"), 2);
    ASSERT_EQ(mmp->waitack_count("queue1"),2);

    MessagePtr msg3 = mmp->front("queue1");
    ASSERT_NE(msg3.get(), nullptr);
    ASSERT_EQ(msg3->payload().body(), std::string("Hello World-3"));
    ASSERT_EQ(mmp->getable_count("queue1"), 1);
    ASSERT_EQ(mmp->waitack_count("queue1"),3);

    MessagePtr msg4 = mmp->front("queue1");
    ASSERT_NE(msg4.get(), nullptr);
    ASSERT_EQ(msg4->payload().body(), std::string("Hello World-4"));
    ASSERT_EQ(mmp->getable_count("queue1"), 0);
    ASSERT_EQ(mmp->waitack_count("queue1"),4);
}
*/

//1. 新增消息测试：新增消息，然后观察可获取消息数量，以及持久化消息数量
TEST(MessageTest, insert_test){
    mymq::BasicProperties properties;
    properties.set_id(UUIDHelper::uuid());
    properties.set_delivery_mode(mymq::DeliveryMode::DURABLE);
    properties.set_routing_key("news.music.pop");
    mmp->insert("queue1", &properties, "Hello World-1", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-2", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-3", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-4", mymq::DeliveryMode::DURABLE);
    mmp->insert("queue1", nullptr, "Hello World-5", mymq::DeliveryMode::UNDURABLE);
    ASSERT_EQ(mmp->getable_count("queue1"), 5);
    ASSERT_EQ(mmp->total_count("queue1"), 4);
    ASSERT_EQ(mmp->durable_count("queue1"), 4);
    ASSERT_EQ(mmp->waitack_count("queue1"), 0);

}
/*
//2. 获取消息测试：获取一条消息，然后在不进行确认的情况下，查看可获取消息数量、待确认消息数量以及测试消息获取的顺序
TEST(MessageTest, select_test){
    ASSERT_EQ(mmp->getable_count("queue1"), 5);
    MessagePtr msg1 = mmp->front("queue1");
    ASSERT_NE(msg1.get(), nullptr);
    ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));
    ASSERT_EQ(mmp->getable_count("queue1"), 4);
    ASSERT_EQ(mmp->waitack_count("queue1"),1);

    MessagePtr msg2 = mmp->front("queue1");
    ASSERT_NE(msg2.get(), nullptr);
    ASSERT_EQ(msg2->payload().body(), std::string("Hello World-2"));
    ASSERT_EQ(mmp->getable_count("queue1"), 3);
    ASSERT_EQ(mmp->waitack_count("queue1"),2);

    MessagePtr msg3 = mmp->front("queue1");
    ASSERT_NE(msg3.get(), nullptr);
    ASSERT_EQ(msg3->payload().body(), std::string("Hello World-3"));
    ASSERT_EQ(mmp->getable_count("queue1"), 2);
    ASSERT_EQ(mmp->waitack_count("queue1"),3);

    MessagePtr msg4 = mmp->front("queue1");
    ASSERT_NE(msg4.get(), nullptr);
    ASSERT_EQ(msg4->payload().body(), std::string("Hello World-4"));
    ASSERT_EQ(mmp->getable_count("queue1"), 1);
    ASSERT_EQ(mmp->waitack_count("queue1"),4);

    MessagePtr msg5 = mmp->front("queue1");
    ASSERT_NE(msg5.get(), nullptr);
    ASSERT_EQ(msg5->payload().body(), std::string("Hello World-5"));
    ASSERT_EQ(mmp->getable_count("queue1"), 0);
    ASSERT_EQ(mmp->waitack_count("queue1"),5);
}
*/
//4. 删除消息测试：确认一条消息，查看持久化消息数量、待确认消息数量
TEST(MessageTest, ack_remove_test){
    ASSERT_EQ(mmp->getable_count("queue1"), 5);
    MessagePtr msg1 = mmp->front("queue1");
    ASSERT_NE(msg1.get(), nullptr);
    ASSERT_EQ(msg1->payload().body(), std::string("Hello World-1"));
    ASSERT_EQ(mmp->getable_count("queue1"), 4);
    ASSERT_EQ(mmp->waitack_count("queue1"), 1);
    mmp->ack("queue1", msg1->payload().properties().id());
    ASSERT_EQ(mmp->waitack_count("queue1"), 0);
    ASSERT_EQ(mmp->durable_count("queue1"), 3);
    ASSERT_EQ(mmp->total_count("queue1"), 4);
}
// 5.销毁测试
TEST(MessageTest, clear_test){
    mmp->destroyQueueMessage("queue1");
}
int main(int argc, char* argv[]){
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MessageTest);
    RUN_ALL_TESTS();
    return 0;
}
